#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <time.h>

#include <TROOT.h>
#include <TFile.h>
#include <TTree.h>
#include <TSQLServer.h>
#include <>

#include "GeomSvc.h"
#include "SRawEvent.h"
#include "SRecEvent.h"

using namespace std;

int main(int argc, char *argv[])
{
  //Initialize geometry service
  Log("Initializing geometry service ... ");
  GeomSvc* geometrySvc = GeomSvc::instance();
#ifdef _USE_R806
  geometrySvc->init("geometry_R806");
#else
  geometrySvc->init("geometry_R746");
#endif

  SRawEvent *rawEvent = new SRawEvent();
  SRecEvent *recEvent = new SRecEvent();

  TFile *dataFile = new TFile(argv[1], "READ");
  TTree *dataTree = (TTree *)dataFile->Get("save");

  dataTree->SetBranchAddress("rawEvent", &rawEvent);
  dataTree->SetBranchAddress("recEvent", &recEvent);

  ///Prepare sql queries
  char query[1024];
  sprintf(query, "DROP DATABASE IF EXISTS %s", argv[2]); con->Exec(query);
  sprintf(query, "CREATE DATABASE %s", argv[2]); con->Exec(query);
  sprintf(query, "USE %s", argv[2]); con->Exec(query);

  const char *kTrackHit_create = "CREATE TABLE kTrackHit("
    "runID           smallint(5) unsigned         NOT NULL,"
    "spillID         mediumint(8) unsigned        NOT NULL,"
    "eventID         int(10) unsigned             NOT NULL,"
    "trackID         int(11) unsigned             NOT NULL,"
    "hitID           bigint(20) unsigned          NOT NULL,"
    "driftSign       tinyint(3)                   NOT NULL,"
    "KEY     runID    (runID)    USING BTREE,"
    "KEY     spillID  (spillID)  USING BTREE,"
    "KEY     eventID  (eventID)  USING BTREE,"
    "KEY     trackID  (trackID)  USING BTREE,"
    "KEY     hitID    (hitID)    USING BTREE"
    ") ENGINE=MyISAM DEFAULT CHARSET=latin1";

  const char *kTrack_create = "CREATE TABLE kTrack("
    "runID           smallint(5) unsigned         NOT NULL,"
    "spillID         mediumint(8) unsigned        NOT NULL,"
    "eventID         int(10) unsigned             NOT NULL,"
    "trackID         int(11) unsigned             NOT NULL,"
    "x0              double                       NOT NULL,"
    "y0              double                       NOT NULL,"
    "z0              double                       NOT NULL,"
    "px0             double                       NOT NULL,"
    "py0             double                       NOT NULL,"
    "py0             double                       NOT NULL,"
    "x1              double                       NOT NULL,"
    "y1              double                       NOT NULL,"
    "z1              double                       NOT NULL,"
    "px1             double                       NOT NULL,"
    "py1             double                       NOT NULL,"
    "py1             double                       NOT NULL,"
    "x3              double                       NOT NULL,"
    "y3              double                       NOT NULL,"
    "z3              double                       NOT NULL,"
    "px3             double                       NOT NULL,"
    "py3             double                       NOT NULL,"
    "py3             double                       NOT NULL,"
    "charge          tinyint(3)                   NOT NULL,"
    "PRIMARY KEY (trackID),"
    "KEY     runID    (runID)    USING BTREE,"
    "KEY     spillID  (spillID)  USING BTREE,"
    "KEY     eventID  (eventID)  USING BTREE,"
    "KEY     trackID  (trackID)  USING BTREE"
    ") ENGINE=MyISAM DEFAULT CHARSET=latin1";

  con->Exec("DROP TABLE IF EXISTS kTrackHit"); con->Exec(kTrackHit_create);
  con->Exec("DROP TABLE IF EXISTS kTrack"); con->Exec(kTrack_create);


  int nEvtMax = dataTree->GetEntries();
  for(int i = offset; i < nEvtMax; i++)
    {
      dataTree->GetEntry(i);
      Log("Processing event " << i << " with eventID = " << rawEvent->getEventID());

      }

  return 1;
}
